}
/**
- * TTLs higher than 30 days will be detected as absolute TTLs
- * (UNIX timestamps), and will result in the cache entry being
- * discarded immediately because the expiry is in the past.
- * Clamp expires >30d at 30d, unless they're >=1e9 in which
- * case they are likely to really be absolute (1e9 = 2011-09-09)
- * @param int $exptime
+ * @param int|float $exptime
* @return int
*/
protected function fixExpiry( $exptime ) {
- return ( $exptime > self::TTL_MONTH && !$this->isRelativeExpiration( $exptime ) )
- ? self::TTL_MONTH
- : (int)$exptime;
+ if ( $exptime < 0 ) {
+ // The PECL driver does not seem to like negative relative values
+ $expiresAt = $this->getCurrentTime() + $exptime;
+ } elseif ( $this->isRelativeExpiration( $exptime ) ) {
+ // TTLs higher than 30 days will be detected as absolute TTLs
+ // (UNIX timestamps), and will result in the cache entry being
+ // discarded immediately because the expiry is in the past.
+ // Clamp expires >30d at 30d, unless they're >=1e9 in which
+ // case they are likely to really be absolute (1e9 = 2011-09-09)
+ $expiresAt = min( $exptime, self::TTL_MONTH );
+ } else {
+ $expiresAt = $exptime;
+ }
+
+ return (int)$expiresAt;
}
}
} elseif ( isset( $params['class'] ) ) {
$class = $params['class'];
// Automatically set the 'async' update handler
- $params['asyncHandler'] = $params['asyncHandler'] ?? 'DeferredUpdates::addCallableUpdate';
+ $params['asyncHandler'] = $params['asyncHandler']
+ ?? [ DeferredUpdates::class, 'addCallableUpdate' ];
// Enable reportDupes by default
$params['reportDupes'] = $params['reportDupes'] ?? true;
// Do b/c logic for SqlBagOStuff
/**
* @covers MediumSpecificBagOStuff::changeTTL
*/
- public function testChangeTTL() {
- $now = 1563892142;
+ public function testChangeTTLRenew() {
+ $now = microtime( true ); // need real time
$this->cache->setMockTime( $now );
$key = $this->cache->makeKey( self::TEST_KEY );
$value = 'meow';
- $this->cache->add( $key, $value, 5 );
+ $this->cache->add( $key, $value, 60 );
$this->assertEquals( $value, $this->cache->get( $key ) );
- $this->assertTrue( $this->cache->changeTTL( $key, 10 ) );
- $this->assertTrue( $this->cache->changeTTL( $key, 10 ) );
+ $this->assertTrue( $this->cache->changeTTL( $key, 120 ) );
+ $this->assertTrue( $this->cache->changeTTL( $key, 120 ) );
$this->assertTrue( $this->cache->changeTTL( $key, 0 ) );
$this->assertEquals( $this->cache->get( $key ), $value );
+
$this->cache->delete( $key );
$this->assertFalse( $this->cache->changeTTL( $key, 15 ) );
+ }
+
+ /**
+ * @covers MediumSpecificBagOStuff::changeTTL
+ */
+ public function testChangeTTLExpireRel() {
+ $now = microtime( true ); // need real time
+ $this->cache->setMockTime( $now );
+
+ $key = $this->cache->makeKey( self::TEST_KEY );
+ $value = 'meow';
+
+ $this->cache->add( $key, $value, 5 );
+ $this->assertTrue( $this->cache->changeTTL( $key, -3600 ) );
+ $this->assertFalse( $this->cache->get( $key ) );
+ }
+
+ /**
+ * @covers MediumSpecificBagOStuff::changeTTL
+ */
+ public function testChangeTTLExpireAbs() {
+ $now = microtime( true ); // need real time
+ $this->cache->setMockTime( $now );
+
+ $key = $this->cache->makeKey( self::TEST_KEY );
+ $value = 'meow';
$this->cache->add( $key, $value, 5 );
$this->assertTrue( $this->cache->changeTTL( $key, $now - 3600 ) );